#region Copyright
/// <copyright>
/// Copyright (c) 2007 Ilia Shramko.
/// Released under the terms of the GNU General Public License.
/// </copyright>
#endregion

using System;
using System.Collections;
using System.Collections.Specialized;
using System.IO;
using System.Text;

namespace Osh.Dvb
{
	/// <summary>
	/// Summary description for EitReader.
	/// </summary>
	public class EitReader : PacketBinaryReader
	{
		private const int FirstEventOffset = 14;
		private const int Crc32Size = 4;

		private DescriptorReader _descriptorReader;

		public EitReader(PacketStream packetStream) : base(packetStream)
		{
			_descriptorReader = new DescriptorReader(packetStream);
		}

		public DescriptorReader DescriptorReader
		{
			get { return _descriptorReader; }
		}

		public EventInformationSection ReadEventInformationSection()
		{
			return new EventInformationSection(this);
		}

		public EitEvent[] ReadEvents(int eventsLoopLength)
		{
			if(eventsLoopLength <= 0)
				return new EitEvent[0];

			ArrayList events = new ArrayList();
			do
			{
				EitEvent eitEvent = new EitEvent(this);
				eventsLoopLength -= eitEvent.Length;
				events.Add(eitEvent);
			}
			while(eventsLoopLength > 0);
			
			return (EitEvent[])events.ToArray(typeof(EitEvent));
		}

		public DateTime ReadDateTime()
		{
			byte d1 = this.ReadByte();
			byte d2 = this.ReadByte();
			byte h = this.ReadByte();
			byte m = this.ReadByte();
			byte s = this.ReadByte();
			return DvbUtil.DecodeDateTime(d1, d2, h, m, s);
		}
		
		public TimeSpan ReadTimeSpan()
		{
			byte h = this.ReadByte();
			byte m = this.ReadByte();
			byte s = this.ReadByte();
			return DvbUtil.DecodeTimeSpan(h, m, s);
		}

	}
}



